//
//  LWCityTabVc.m
//  LWShopItem
//
//  Created by mac on 17/6/8.
//  Copyright © 2017年 LWShop.com. All rights reserved.
//

#import "LWCityTabVc.h"
#import "UIColor+Addition.h"
#import <AFNetworking.h>
#import "LWProvinceModel.h"
#import <MJExtension.h>

static NSString *cellLd = @"cellLd";

@interface LWCityTabVc ()




@end

@implementation LWCityTabVc
{
    NSMutableArray <LWProvinceModel *>*_CityList;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self loadData];
}
#pragma MARK - UI
- (void)setupUI
{
    self.title = @"请选择所在城市";
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHex:0x9ACD32];
    UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(popClick)];
    self.navigationItem.leftBarButtonItem = backBtnItem;
    
    self.tableView.rowHeight = 50;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellLd];
    
}
#pragma MARK - DATA
- (void)loadData
{
    
    AFHTTPSessionManager  *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    dic[@"province"] = self.ID;
    
    [manager POST:@"http://shop.sdlinwang.com/index.php?m=Api&c=User&a=get_city" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        
        NSLog(@"请求成功JSON:%@", JSON);
        
        _CityList = [LWProvinceModel mj_objectArrayWithKeyValuesArray:JSON[@"result"]];
        
        
        [self.tableView reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
        
    }];
    

    
}
#pragma MARK - POP
- (void)popClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _CityList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellLd forIndexPath:indexPath];
    
    LWProvinceModel *cityModel = _CityList[indexPath.row];
    
    cell.textLabel.text = cityModel.name;
    
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"点击了第%ld",(long)indexPath.row);
    
    LWProvinceModel *model = _CityList[indexPath.row];
    
    self.completion(model);
    
    NSLog(@"-----%@----",model.id);
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
}



@end
